package com.qf.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.qf.annotation.RequiresPermissionsDesc;
import com.qf.pojo.DtsPermission;
import com.qf.pojo.DtsRole;
import com.qf.service.DtsPermissionService;
import com.qf.service.DtsRoleService;
import com.qf.util.Permission;
import com.qf.util.PermissionUtil;
import com.qf.util.ResponseUtil;
import com.qf.vo.PermVo;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.web.bind.annotation.*;

import javax.swing.*;
import java.util.*;
import java.util.stream.Collectors;

/**
 * @author YDW
 * projectName:shop-manage
 * time:2022/5/19 22:27
 * description:
 */
@RestController
@RequestMapping("/admin/role")
public class AdminRoleController {

    @Autowired
    DtsRoleService roleService;

    @Autowired
    ApplicationContext context;

    @Autowired
    DtsPermissionService permissionService;

    @GetMapping("/options")
    public Object options(){
        //查询所有角色
        List<DtsRole> roles= roleService.findAll();
        //转换前端需要的角色
        List<Map<String, Object>> data = roles.stream().map(role -> {
            Map<String, Object> map = new HashMap<>();
            map.put("value", role.getId());
            map.put("label", role.getName());
            return map;
        }).collect(Collectors.toList());
        return ResponseUtil.ok(data);
    }

    @RequiresPermissions("admin:role:list")
    @RequiresPermissionsDesc(menu = { "系统管理", "角色管理" },button = "角色查询")
    @GetMapping("/list")
    public Object list(
            @RequestParam(name = "page",required = false,defaultValue = "1") Integer page,
            @RequestParam(name = "limit",required = false,defaultValue = "10") Integer limit,
            String sort,String order, String rolename
    ){
        IPage<DtsRole> ipage=roleService.queryPage(page,limit,sort,order,rolename);
        Map<String,Object> data=new HashMap<>();
        data.put("total",ipage.getTotal());
        data.put("items",ipage.getRecords());
        return ResponseUtil.ok(data);
    }

    @RequiresPermissions("admin:role:create")
    @RequiresPermissionsDesc(menu = { "系统管理", "角色管理" },button = "角色添加")
    @PostMapping("/create")
    public Object create(@RequestBody DtsRole inrole){
        DtsRole role= null;
        try {
            role = roleService.add(inrole);
        } catch (Exception e) {
            return ResponseUtil.fail(-1,e.getMessage());
        }
        return ResponseUtil.ok(role);
    }

    @RequiresPermissions("admin:role:update")
    @RequiresPermissionsDesc(menu = { "系统管理", "角色管理" }, button = "角色编辑")
    @PostMapping("/update")
    public Object update(@RequestBody DtsRole inrole){
        try {
            DtsRole role=roleService.update(inrole);
        } catch (Exception e) {
            return ResponseUtil.fail(-1,e.getMessage());
        }
        return ResponseUtil.ok();
    }

    @RequiresPermissions("admin:role:delete")
    @RequiresPermissionsDesc(menu = { "系统管理", "角色管理" }, button = "角色删除")
    @PostMapping("/delete")
    public Object delete(@RequestBody DtsRole role){
        boolean flag=roleService.delete(role);
        if(flag){
            return ResponseUtil.ok();
        }else {
            return ResponseUtil.fail(-1,"该角色已被绑定，不能删除");
        }
    }

    @RequiresPermissions("admin:role:permission:get")
    @RequiresPermissionsDesc(menu = { "系统管理", "角色管理" }, button = "权限详情")
    @GetMapping("/permissions")
    public Object permissions(Integer roleId){
        //获取控制器所有方法上的权限列表
        List<Permission> permissions = PermissionUtil.listPermission(context, "com.qf.controller");
        //获得所有的权限字符串列表
        Set<String> permissionStrs = PermissionUtil.listPermissionString(permissions);
        //把所有权限信息转换为前端需要的数据格式
        List<PermVo> systemPermissions = PermissionUtil.listPermVo(permissions);
        Map<String,Object> data=new HashMap<>();
        //所有的系统权限
        data.put("systemPermissions",systemPermissions);
        //该角色拥有的权限

        //如果该角色为超级管理员，拥有所有权限
        if(roleId==1){
            data.put("assignedPermissions",permissionStrs);
        }else {
            //如果不是超级管理员，则应该查询指定的权限
            Set<String> perms = permissionService.findPermByIds(new Integer[]{roleId});
            data.put("assignedPermissions",perms);
        }
        return ResponseUtil.ok(data);

    }

    @RequiresPermissions("admin:role:permission:update")
    @RequiresPermissionsDesc(menu = { "系统管理", "角色管理" }, button = "权限变更")
    @PostMapping("/permissions")
    public Object grantPermission(@RequestBody Map<String,Object> map ){
        //取出map中的数据
        //接收角色id
        Integer roleId = (Integer) map.get("roleId");
        //接收赋予的权限
        ArrayList<String> permissions=(ArrayList) map.get("permissions");

        if(roleId==1){
            return ResponseUtil.fail(-1,"超级管理员权限不能修改");
        }
        permissionService.updatePermission(roleId,permissions);

        return ResponseUtil.ok();
    }
}
